package com.aps.apspda.activity;

import android.os.Handler;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.aps.apspda.R;
import com.aps.apspda.adapter.EqpThingListAdapter;
import com.aps.apspda.adapter.InputDataListAdapter;
import com.aps.apspda.base.BaseActivity;
import com.aps.apspda.callback.DialogEntityCallBack;
import com.aps.apspda.callback.EntityCallBack;
import com.aps.apspda.callback.SuccessAndFailCallback;
import com.aps.apspda.dialog.CTsensorFragmentDialog;
import com.aps.apspda.entity.BaseEntity;
import com.aps.apspda.entity.ChangeThingInputDataEntity;
import com.aps.apspda.entity.InputDataEntity;
import com.aps.apspda.entity.MaterialInfoBean;
import com.aps.apspda.entity.WaferSawEntity;
import com.aps.apspda.myview.ClearEditText;
import com.aps.apspda.myview.TitleView;
import com.aps.apspda.utils.AntiShake;
import com.aps.apspda.utils.AppUtils;
import com.aps.apspda.utils.NetUtils;
import com.aps.apspda.utils.StaticMembers;
import com.aps.apspda.utils.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;

public class ThingChangeActivity extends BaseActivity {

    @BindView(R.id.topView)
    TitleView topView;
    @BindView(R.id.etRemark)
    ClearEditText etRemark;
    @BindView(R.id.tvEqpId)
    TextView tvEqpId;
    @BindView(R.id.rvEqpList)
    RecyclerView rvEqpList;
    @BindView(R.id.rvInputDataList)
    RecyclerView rvInputDataList;
    @BindView(R.id.etCapillarySeqNo)
    ClearEditText etCapillarySeqNo;
    @BindView(R.id.etCapillaryMaterialPartNo)
    ClearEditText etCapillaryMaterialPartNo;
    @BindView(R.id.llCapillary)
    LinearLayout llCapillary;


    private List<MaterialInfoBean> materialinfoBeanList;
    private EqpThingListAdapter eqpThingListAdapter;
    private MaterialInfoBean bean;
    private String type;
    private boolean isAllow = false;
    private boolean isYingYuan = false;
    private boolean isRuanYuan = false;
    private List<InputDataEntity.InputObject> inputList;
    private boolean isNeedVerify = true;
    private String seqNos;
    private String eqpType;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_thing_change;
    }
    @Override
    protected void initView() {
        topView.setTitleMode(TitleView.NORMAL_TEXT_MODE);
        topView.setRightListener("确定更换", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (AntiShake.check(v.getId())) {    //判断是否多次点击
                    ToastUtils.showFreeToast("请勿重复点击",
                            ThingChangeActivity.this, false, Toast.LENGTH_SHORT);
                    return;
                }
                if (materialinfoBeanList.size() == 0) {
                    ToastUtils.showFreeToast("请先扫描物料条码",
                            ThingChangeActivity.this, false, Toast.LENGTH_SHORT);
                    return;
                }
                if (type.equals("Capillary")) {
                    if (etCapillarySeqNo.getText().toString().trim().length() != 0 &&
                            etCapillaryMaterialPartNo.getText().toString().trim().length() != 0) {
                        isAllow = true;
                    } else {
                        isAllow = false;
                    }
                }
                if (isAllow || !isNeedVerify) {
                    if (!isNeedVerify && bean.getSeqNo().equals("请选择")) {
                        ToastUtils.showFreeToast("请选择型号",
                                ThingChangeActivity.this, false, Toast.LENGTH_SHORT);
                        return;
                    }
                     /* if(bean.getSeqNo()==null||bean.getSeqNo().equals("")||bean.getSeqNo()==""){
                        ToastUtils.showFreeToast("型号不能为空!",
                                ThingChangeActivity.this, false, Toast.LENGTH_SHORT);
                        return;
                    }
                    String aa = bean.getSeqNo().trim();
                    if(bean.getSeqNo().trim()==null||bean.getSeqNo().trim()==""||bean.getSeqNo().trim().equals("")){
                        ToastUtils.showFreeToast("型号不能全部输入空格!",
                                ThingChangeActivity.this, false, Toast.LENGTH_SHORT);
                        return;
                    }*/
                    boolean isOk = true;
                    for (InputDataEntity.InputObject inputObject : inputList) {
                        if (inputObject.getISNULL().equalsIgnoreCase("Y")) {
                            if (inputObject.getDATAVALUE().length() == 0) {
                                isOk = false;
                                ToastUtils.showFreeToast("请填写带*栏数据", ThingChangeActivity.this, false, Toast.LENGTH_SHORT);
                                break;
                            } else {
                                if (inputObject.getDATAVALUE().toUpperCase().contains("NG")) {
                                    String[] strs = inputObject.getDATAVALUE().split(";");
                                    if (strs.length <= 1) {
                                        isOk = false;
                                        ToastUtils.showFreeToast("请给NG项填写原因", ThingChangeActivity.this, false, Toast.LENGTH_SHORT);
                                    }
                                }
                            }
                        }
                    }
                    if (isOk) {
                        requestSubmit("change");
                    }
                } else {
                    ToastUtils.showFreeToast("请检查扫描的物料条码是否正确",
                            ThingChangeActivity.this, false, Toast.LENGTH_LONG);
                }
            }
        });
        rvEqpList.setLayoutManager(new LinearLayoutManager(ThingChangeActivity.this));
        rvEqpList.setItemAnimator(new DefaultItemAnimator());
        rvEqpList.addItemDecoration(new DividerItemDecoration(ThingChangeActivity.this, 1));
        rvInputDataList.setLayoutManager(new LinearLayoutManager(ThingChangeActivity.this));
        rvInputDataList.setItemAnimator(new DefaultItemAnimator());
        rvInputDataList.addItemDecoration(new DividerItemDecoration(ThingChangeActivity.this, 1));

        materialinfoBeanList = new ArrayList<>();
        eqpThingListAdapter = new EqpThingListAdapter(materialinfoBeanList, null);
        eqpThingListAdapter.setIsShowButton(false);
        eqpThingListAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        eqpThingListAdapter.isFirstOnly(true);
        rvEqpList.setAdapter(eqpThingListAdapter);

        bean = (MaterialInfoBean)
                getIntent().getSerializableExtra("mEntity");
        bean.setSeqNo(null);
        //bean.setMaterialLotNo(null);
        bean.setMaterialPartNo(null);
        topView.setTitle(AppUtils.getTypeZH(bean.getType()) + "更换(" + StaticMembers.CUR_USER.getUSERNAME() + ")");
        type = bean.getType();

        if (type.equals("Capillary")) {
            llCapillary.setVisibility(View.VISIBLE);
            etRemark.setVisibility(View.GONE);
        } else if (type.equals("Collet") || type.equals("RubberTip") ||
                type.equals("EjectorPin") || type.equals("StampingPin") || type.equals("DispenseTool")) {
            isNeedVerify = false;
            llCapillary.setVisibility(View.GONE);
            etRemark.setVisibility(View.GONE);
            rvInputDataList.setVisibility(View.VISIBLE);
        } else {
            setCallback(etRemark);
            llCapillary.setVisibility(View.GONE);
            etRemark.setVisibility(View.VISIBLE);
        }
        tvEqpId.setText(getEqpID());

        etCapillaryMaterialPartNo.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (start == 0 && before == 0 && count > 1) {
                    if (etCapillaryMaterialPartNo.getText().toString().trim().length() != 0) {
                        if (s.length() > etCapillaryMaterialPartNo.getText().toString().trim().length()) {
                            ToastUtils.showFreeToast("料号长度应该小于序列号长度，请检查",
                                    ThingChangeActivity.this, false, Toast.LENGTH_LONG);
                            //etCapillaryMaterialPartNo.setText("");
                            etCapillaryMaterialPartNo.setText("");
                            return;
                        }
                        if (s.toString().equals(etCapillarySeqNo.getText().toString().trim())) {
                            ToastUtils.showFreeToast("料号与序列号相同，请检查",
                                    ThingChangeActivity.this, false, Toast.LENGTH_LONG);
                            bean.setSeqNo(null);
                            etCapillaryMaterialPartNo.setText("");
                            //etCapillarySeqNo.setText("");
                            return;
                        }
                        if (materialinfoBeanList.size() != 0) {
                            materialinfoBeanList.get(0).setMaterialPartNo(s.toString());
                        } else {
                            bean.setMaterialPartNo(s.toString());
                            materialinfoBeanList.add(bean);
                        }
                        rvInputDataList.setVisibility(View.VISIBLE);
                        eqpThingListAdapter.notifyDataSetChanged();

                    }
                    //ACB3000用
                  /*  etCapillaryMaterialPartNo.setText("");
                    etCapillarySeqNo.setText("");*/

                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });


        etCapillarySeqNo.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (start == 0 && before == 0 && count > 1) {
                    if (etCapillarySeqNo.getText().toString().trim().length() != 0) {
                        if(etCapillaryMaterialPartNo.getText().toString().trim().length()==0){
                            ToastUtils.showFreeToast("请先扫描料号，再扫描序列号!",
                                    ThingChangeActivity.this, false, Toast.LENGTH_LONG);
                            bean.setMaterialPartNo(null);
                            etCapillarySeqNo.setText("");
                            return;
                        }
                        if (s.length() < etCapillarySeqNo.getText().toString().trim().length()) {
                            ToastUtils.showFreeToast("序列号长度应大于料号长度，请检查",
                                    ThingChangeActivity.this, false, Toast.LENGTH_LONG);
                            etCapillarySeqNo.setText("");
                            return;
                        }
                        if (s.toString().equals(etCapillaryMaterialPartNo.getText().toString().trim())) {
                            ToastUtils.showFreeToast("料号与序列号相同，请检查",
                                    ThingChangeActivity.this, false, Toast.LENGTH_LONG);
                            bean.setMaterialPartNo(null);
                            etCapillarySeqNo.setText("");
                            return;
                        }
                        if (materialinfoBeanList.size() != 0) {
                            materialinfoBeanList.get(0).setSeqNo(s.toString());
                        } else {
                            bean.setSeqNo(s.toString());
                            materialinfoBeanList.add(bean);
                        }
                        rvInputDataList.setVisibility(View.VISIBLE);
                        requestSubmit("check");
                        eqpThingListAdapter.notifyDataSetChanged();
                    }
                    //ACB3000用
                  /*  etCapillaryMaterialPartNo.setText("");
                    etCapillarySeqNo.setText("");*/
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });


        etRemark.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                return event != null && (event.getKeyCode() == KeyEvent.KEYCODE_ENTER);
            }
        });

        etRemark.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (start == 0 && before == 0 && count > 1) {
                    if (s.length() > 0) {
                        //扫描到数据 然后请求接口
                        isAllow = isAllow(s.toString());
                        if (type.equals("HubBlade") || type.equals("HubBladeYZ1") || type.equals("HubBladeYZ2")) {
                            if (!isYingYuan) {
                                if (isAllow) {
                                    materialinfoBeanList.clear();
                                    materialinfoBeanList.add(bean);
                                    eqpThingListAdapter.notifyDataSetChanged();
                                    requestSubmit("check");
                                    rvInputDataList.setVisibility(View.VISIBLE);
                                    verifyCtsensor();
                                } else {
                                    materialinfoBeanList.clear();
                                    eqpThingListAdapter.notifyDataSetChanged();
                                    ToastUtils.showFreeToast("请检查扫描的物料条码是否正确",
                                            ThingChangeActivity.this, false, Toast.LENGTH_LONG);
                                }
                            }
                        } else if (type.equals("HublessBlade")) {
                            if (!isRuanYuan) {
                                if (isAllow) {
                                    materialinfoBeanList.clear();
                                    materialinfoBeanList.add(bean);
                                    eqpThingListAdapter.notifyDataSetChanged();
                                    requestSubmit("check");
                                    rvInputDataList.setVisibility(View.VISIBLE);
                                    verifyCtsensor();
                                } else {
                                    materialinfoBeanList.clear();
                                    eqpThingListAdapter.notifyDataSetChanged();
                                    rvInputDataList.setVisibility(View.GONE);
                                    ToastUtils.showFreeToast("请检查扫描的物料条码是否正确",
                                            ThingChangeActivity.this, false, Toast.LENGTH_LONG);
                                }
                            }
                        } else {
                            if (isAllow) {
                                materialinfoBeanList.clear();
                                materialinfoBeanList.add(bean);
                                eqpThingListAdapter.notifyDataSetChanged();
                                requestSubmit("check");
                                rvInputDataList.setVisibility(View.VISIBLE);
                            } else {
                                materialinfoBeanList.clear();
                                eqpThingListAdapter.notifyDataSetChanged();
                                rvInputDataList.setVisibility(View.GONE);
                                ToastUtils.showFreeToast("请检查扫描的物料条码是否正确",
                                        ThingChangeActivity.this, false, Toast.LENGTH_LONG);
                            }
                        }
                    } else {
                        if (!type.toLowerCase().contains("hubblade")) {
                            rvInputDataList.setVisibility(View.GONE);
                        }
                        materialinfoBeanList.clear();
                        eqpThingListAdapter.notifyDataSetChanged();
                        ToastUtils.showFreeToast("扫描信息有误", ThingChangeActivity.this, false, Toast.LENGTH_SHORT);
                    }
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    private void requestSubmit(final String submitType) {
        EntityCallBack<BaseEntity<Object>> callBack = new DialogEntityCallBack<BaseEntity<Object>>
                (new TypeToken<BaseEntity<Object>>() {
                }.getType(), getSupportFragmentManager(), this) {

            @Override
            public void onSuccess
                    (final Response<BaseEntity<Object>> response) {
                if (response.body().isSuccess(ThingChangeActivity.this)) {
                    if (submitType.equals("change")) {
                        new Handler().postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                ThingChangeActivity.this.finish();
                            }
                        }, 1000);
                    } else {
                        isAllow = true;
                    }
                    loadSuccess(response.body().getMessage());
                } else {
                    //ACB3000用
                  /*  bean.setMaterialPartNo(null);
                    bean.setSeqNo(null);*/
                    loadFail(response.body().getMessage());
                    if (submitType.equals("check")) {
                        isAllow = false;
                        materialinfoBeanList.clear();
                        if (!type.toLowerCase().contains("hubblade")) {
                            rvInputDataList.setVisibility(View.GONE);
                        }
                        eqpThingListAdapter.notifyDataSetChanged();
                    }
                }
            }

            @Override
            public void onError
                    (Response<BaseEntity<Object>> response) {
                super.onError(response);
                loadError(response.getException(), "ChangeMeterial");
            }
        };

        Map<String, String> map = new HashMap<>();
        map.put("EqpId", getEqpID());
        map.put("OPID", StaticMembers.CUR_USER.getUSERNAME());
        map.put("Name", StaticMembers.CUR_USER.getUSERNAME());
        map.put("Type", bean.getType());
        map.put("Size", bean.getSize());
        map.put("RemainTime", bean.getRemainTime());
        map.put("SeqNo", bean.getSeqNo());
        map.put("MaterialPartNo", bean.getMaterialPartNo());
        // map.put("MaterialPartNo",etCapillaryMaterialPartNo.getText().toString().trim());
        map.put("Device", bean.getDevice());
        map.put("WaferSource", bean.getWaferSource());
        map.put("Diagram", bean.getDiagram());
        map.put("ThawEndTime", bean.getThawEndTime());
        map.put("SawLength", bean.getSawLength());
        map.put("SawThickness", bean.getSawThickness());
        map.put("ExternalDiameter", bean.getExternalDiameter());
        map.put("InternalDiameter", bean.getInternalDiameter());
        map.put("MaterialLotNo", bean.getMaterialLotNo());
        map.put("EndTime", bean.getEndTime());
        map.put("StartTime", bean.getStartTime());
        map.put("ChangeMeterialType", submitType);
        if (submitType.equals("change")) {
            JSONObject jsonObject = new JSONObject();
            try {
                if (inputList.size() == 0) {
                    jsonObject.put("INPUTDATA", "");
                } else {
                    JSONArray jsonArray = new JSONArray(new Gson().toJson(inputList));
                    jsonObject.put("INPUTDATA", jsonArray);
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
            map.put("InputDataList", jsonObject.toString());
        }
        NetUtils.requestNet(this, "/ChangeMeterial", map, callBack);
    }

    public boolean isAllow(String str) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        if (type.equals("Collet") || type.equals("RubberTip") ||
                type.equals("EjectorPin") || type.equals("StampingPin") || type.equals("DispenseTool")) {
            String[] strs = str.split(";");
            if (strs.length == 2) {
                bean.setSeqNo(strs[0]);
                bean.setMaterialPartNo(strs[1]);
                return true;
            } else {
                return false;
            }
        } else if (("LeftPaste").equals(type) || ("RightPaste").equals(type) || type.equals("Paste")) {
            String[] strs2 = str.split(";");
            if (strs2.length == 6) {
                bean.setEndTime(strs2[2]);
                bean.setThawEndTime(strs2[1]);
                bean.setMaterialPartNo(strs2[0]);
                bean.setSeqNo(strs2[3]);
                return true;
            } else {
                return false;
            }
        } else if (type.equals("Leadframe")) {
            if(str.contains("SHEC;")){
                String strRep = str.substring(5);
                String[] strs2 = strRep.split(";");
                if (strs2.length == 6) {
                 bean.setMaterialLotNo(strs2[1]);
                 bean.setMaterialPartNo(strs2[0]);
                 bean.setEndTime(strs2[5]);
                    return true;
                } else {
                    return false;
                }
            }
                String[] strs2 = str.split(";");
//            if (strs2.length == 6) {
//                bean.setSeqNo(strs2[0]);
//                bean.setMaterialPartNo(strs2[2].replace("1P", ""));
//                bean.setMaterialLotNo(strs2[3].replace("1T", ""));
//                bean.setStartTime(strs2[4].replace("9D", ""));
//                return true;
//            }
            if (strs2.length == 9) {
                bean.setMaterialPartNo(strs2[1]);
                bean.setSeqNo(strs2[2]);
                bean.setEndTime(strs2[8]);
                return true;
            } else {
                return false;
            }
        } else if (type.equals("Wire")) {
            String[] strs = str.split(";");
            if (strs.length == 9) {
                bean.setSize(strs[0]);
                bean.setMaterialPartNo(strs[1]);
                bean.setSeqNo(strs[2]);
                bean.setThawEndTime(strs[4]);
                bean.setRemainTime(strs[3]);
              /*  String aa = strs[8];
                String EndTime= format.format(aa);
                bean.setEndTime(EndTime);*/
                bean.setEndTime(strs[8]);
                return true;
            } else {
                return false;
            }
        } else if (type.equals("HublessBlade")) {
            isRuanYuan = false;
            String[] strs;
            if (str.contains(";")) {
                strs = str.split(";");
            } else {
                strs = new String[1];
                strs[0] = str;
            }
            if (strs.length == 5) {
                bean.setMaterialPartNo(strs[0]);
                String str1 = strs[1].replace(" ", "");
                bean.setExternalDiameter(str1.substring(0, str1.length() - 1));
                String str2 = strs[2].replace(" ", "");
                bean.setInternalDiameter(str2.substring(0, str2.length() - 1));
                String str3 = strs[3].replace(" ", "");
                bean.setSawThickness(str3.substring(0, str3.length() - 1));
                bean.setSeqNo(strs[4]);
                return true;
            } else if (strs.length == 1 && strs[0].length() == 15) {
                isRuanYuan = true;
                str = str.substring(0, str.length() - 3);
                requestHubBlade(str);
                return true;
            } else {
                return false;
            }
        } else if (type.equals("Wafer")) {
            String[] strs = str.split("\\^");
            if (strs.length == 4) {
                bean.setDevice(strs[0]);
                bean.setWaferSource(strs[1]);
                bean.setDiagram(strs[2]);
                bean.setMaterialPartNo(strs[3]);
                return true;
            } else {
                return false;
            }
        } else if (type.equals("Epoxy") || type.equals("EpoxyLeft") || type.equals("EpoxyRight")) {
            String[] strs = str.split(";");
            if (strs.length == 5) {
                bean.setMaterialPartNo(strs[0]);
                bean.setThawEndTime(strs[1]);
                bean.setEndTime(strs[2]);
                bean.setSeqNo(strs[3]);
                return true;
            } else {
                return false;
            }
        } else if (type.equals("Block")) {
            if (str.length() == 2) {
                str = str.replace("0", "");
                if (AppUtils.isNumeric(str)) {
                    bean.setMaterialPartNo(str);
                    return true;
                } else {
                    return false;
                }
            } else {
                return false;
            }
        } else if (type.equals("HubBlade") || type.equals("HubBladeYZ1") || type.equals("HubBladeYZ2")) {
            isYingYuan = false;
            String[] strs;
            if (str.contains(";")) {
                strs = str.split(";");
            } else {
                strs = new String[1];
                strs[0] = str;
            }
          /*  if (strs.length == 4) {
                bean.setMaterialPartNo(strs[0]);
                bean.setSawLength(strs[1]);
                bean.setSawThickness(strs[2]);
                bean.setSeqNo(strs[3]);
                return true;
            }
            else*/
            if (strs.length == 5) {
                bean.setMaterialPartNo(strs[0]);
                bean.setSawLength(strs[1]);
                bean.setSawThickness(strs[2]);
                bean.setSeqNo(strs[4]);
                return true;
            } else if (strs.length == 1 && str.length() > 15) {
                if (str.substring(str.length() - 15, str.length() - 14).equals("H")) {
                    isYingYuan = true;
                    str = str.substring(str.length() - 15, str.length() - 3);
                    requestHubBlade(str);
                    return true;
                } else if (str.substring(str.length() - 16, str.length() - 15).equals("H")) {
                    isYingYuan = true;
                    str = str.substring(0, str.length() - 1);
                    str = str.substring(str.length() - 15, str.length() - 3);
                    requestHubBlade(str);
                    return true;
                } else {
                    return false;
                }
            } else {
                return false;
            }
        } else {
            return false;
        }
    }


    private void verifyCtsensor() {
        if (!StaticMembers.isOffLine) {
            if (!type.equals("HubBladeYZ1") && !type.equals("HubBladeYZ2")) {
                final CTsensorFragmentDialog cTsensorFragmentDialog = new CTsensorFragmentDialog();
                cTsensorFragmentDialog.setCallBackAndWhere(new SuccessAndFailCallback() {
                    @Override
                    public void onSuccess() {

                    }

                    @Override
                    public void onFail() {
                        isAllow = false;
                        materialinfoBeanList.clear();
                        if (!type.toLowerCase().contains("hubblade")) {
                            rvInputDataList.setVisibility(View.GONE);
                        }
                        eqpThingListAdapter.notifyDataSetChanged();
                    }
                }, bean);
                cTsensorFragmentDialog.show(getSupportFragmentManager(), "cTsensor_dialog");
            }
        }
    }

    private void requestHubBlade(String str) {
        EntityCallBack<BaseEntity<WaferSawEntity>> callBack = new DialogEntityCallBack<BaseEntity<WaferSawEntity>>
                (new TypeToken<BaseEntity<WaferSawEntity>>() {
                }.getType(), getSupportFragmentManager(), this) {

            @Override
            public void onSuccess
                    (final Response<BaseEntity<WaferSawEntity>> response) {

                if (response.body().isSuccess(ThingChangeActivity.this)) {
                    isAllow = true;
                    bean.setMaterialPartNo(response.body().getData().getMATERIALPARTNO());
                    bean.setSawLength(response.body().getData().getSAWLENGTH());
                    bean.setSawThickness(response.body().getData().getSAWTHICKNESS());
                    bean.setSeqNo(response.body().getData().getSEQNO());
                    bean.setInternalDiameter(response.body().getData().getINTERNALDIAMETER());
                    bean.setExternalDiameter(response.body().getData().getEXTERNALDIAMETER());
                    if (isAllow) {
                        materialinfoBeanList.clear();
                        materialinfoBeanList.add(bean);
                        eqpThingListAdapter.notifyDataSetChanged();
                        requestSubmit("check");
                        rvInputDataList.setVisibility(View.VISIBLE);
                        verifyCtsensor();
                    } else {
                        materialinfoBeanList.clear();
                        if (!type.toLowerCase().contains("hubblade")) {
                            rvInputDataList.setVisibility(View.GONE);
                        }
                        eqpThingListAdapter.notifyDataSetChanged();
                        ToastUtils.showFreeToast("请检查扫描的物料条码是否正确",
                                ThingChangeActivity.this, false, Toast.LENGTH_LONG);
                    }
                } else {
                    isAllow = false;
                    //bunny 物料验证失败后清空内容
                    materialinfoBeanList.clear();
                    eqpThingListAdapter.notifyDataSetChanged();
                    loadFail(response.body().getMessage());
                }
            }

            @Override
            public void onError
                    (Response<BaseEntity<WaferSawEntity>> response) {
                super.onError(response);
                isAllow = false;
                loadError(response.getException(), "WaferSaw");
            }
        };

        Map<String, String> map = new HashMap<>();
        map.put("SeqNo", str);

        map.put("EqpId", getEqpID());
        map.put("OPID", StaticMembers.CUR_USER.getUSERNAME());
        map.put("Name", StaticMembers.CUR_USER.getUSERNAME());
        map.put("Type", bean.getType());
        map.put("Size", bean.getSize());
        map.put("RemainTime", bean.getRemainTime());
        // map.put("SeqNo", bean.getSeqNo());
        map.put("MaterialPartNo", bean.getMaterialPartNo());
        // map.put("MaterialPartNo",etCapillaryMaterialPartNo.getText().toString().trim());
        map.put("Device", bean.getDevice());
        map.put("WaferSource", bean.getWaferSource());
        map.put("Diagram", bean.getDiagram());
        map.put("ThawEndTime", bean.getThawEndTime());
        map.put("SawLength", bean.getSawLength());
        map.put("SawThickness", bean.getSawThickness());
        map.put("ExternalDiameter", bean.getExternalDiameter());
        map.put("InternalDiameter", bean.getInternalDiameter());
        map.put("MaterialLotNo", bean.getMaterialLotNo());
        map.put("EndTime", bean.getEndTime());
        map.put("StartTime", bean.getStartTime());
        map.put("ChangeMeterialType", "CHECK");
        if (type.toLowerCase().contains("hubblade")) {
            map.put("Materialtype", "HubBlade");
        } else {
            map.put("Materialtype", type);
        }
        NetUtils.requestNet(this, "/WaferSaw", map, callBack);
    }

    @Override
    protected void initData() {
        eqpType = getIntent().getStringExtra("eqpType");
        requestInputData();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        OkGo.getInstance().cancelTag(this);
    }

    private void requestInputData() {
        EntityCallBack<BaseEntity<ChangeThingInputDataEntity>> callBack = new DialogEntityCallBack<BaseEntity<ChangeThingInputDataEntity>>
                (new TypeToken<BaseEntity<ChangeThingInputDataEntity>>() {
                }.getType(), getSupportFragmentManager(), this) {

            @Override
            public void onSuccess
                    (final Response<BaseEntity<ChangeThingInputDataEntity>> response) {
                if (response.body().isSuccess(ThingChangeActivity.this)) {
                    if (!isNeedVerify) {
                        bean.setSeqNo(response.body().getData().getSeqNo());
                        materialinfoBeanList.clear();
                        materialinfoBeanList.add(bean);
                        eqpThingListAdapter.setIsSeqNoPullDown();
                        eqpThingListAdapter.notifyDataSetChanged();
                    }
                    dealInputList(response.body().getData().getINPUTDATALIST().getINPUTDATA());
                    InputDataListAdapter inputDataListAdapter = new InputDataListAdapter(inputList);
                    inputDataListAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
                    inputDataListAdapter.isFirstOnly(true);
                    rvInputDataList.setAdapter(inputDataListAdapter);
                    if (type.toLowerCase().contains("hubblade")) {
                        rvInputDataList.setVisibility(View.VISIBLE);
                    }
                } else {
                    loadFail(response.body().getMessage());
                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            ThingChangeActivity.this.finish();
                        }
                    }, 2000);
                }
            }

            @Override
            public void onError
                    (Response<BaseEntity<ChangeThingInputDataEntity>> response) {
                super.onError(response);
                loadError(response.getException(), "ShowMeterialInputData");
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        ThingChangeActivity.this.finish();
                    }
                }, 2000);
            }
        };

        Map<String, String> map = new HashMap<>();
        map.put("EqpId", getEqpID());
        map.put("USERNAME", StaticMembers.CUR_USER.getUSERNAME());
        map.put("Type", type);
        NetUtils.requestNet(this, "/ShowMeterialInputData", map, callBack);
    }

    private void dealInputList(List<InputDataEntity.InputObject> inputdata) {
        inputList = new ArrayList<>();
        if (StaticMembers.isOffLine) {
            inputList.addAll(inputdata);
        } else {
            List<InputDataEntity.InputObject> removeList = new ArrayList<>();
            for (InputDataEntity.InputObject object : inputdata) {
                if (object.getEQPModeType().equals("Y")) {
                    removeList.add(object);
                }
            }
            inputdata.removeAll(removeList);
            inputList.addAll(inputdata);
        }
    }
}
